// pages/index/details.js
var api = require('../../api.js'),
  app = getApp(),
  WxParse = require('../../wxParse/wxParse.js'),
  WxParses = require('../../wxParse/html2json.js');
var nowdate = new Date()
var nowTime = nowdate.getFullYear() + '-' + (nowdate.getMonth() + 1) +'-' +nowdate.getDate() 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: 1,
    commont: 0,
    zujian: [],
    indexs: 0,
    date: [],
    uid: wx.getStorageSync("user").uid
  },
  bindDateChange: function(e) {
    var that = this
    // 当前选择的 时间线数组 下标
    var dateInd = e.currentTarget.dataset.ind
    // 当前选择的 最新时间
    var date = e.detail.value;
    that.data.date.splice(dateInd, 1, date)
    that.setData({
      date: that.data.date
    })
  },
  formSubmit: function(e) {
    console.log(e.detail.value);
    var _that = this;
    var articleid = _that.data.articleid;
    var user = wx.getStorageSync("user")
    var uid = user.uid;
    var data = e.detail.value;
    data.uid = uid;
    data.assignmentId = articleid;
    console.log(data);
    // return false;
    app.request({
      url: api.infoCenter.biaodan,
      method: "POST",
      data: data,
      success: function(t) {
        console.log(t);
        if (t.isadd) {
          wx.showModal({
            title: '提示',
            content: '添加成功',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '添加失败',
          })
        }
      },
      fail: function() {
        console.log("fail")
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      articleid: options.articleid
    })
    this.infoDetail(options)
  },
  // 信息详情
  infoDetail: function(options) {
    var user = wx.getStorageSync("user")
    var _that = this
    // let that = this
    console.log(user)
    app.request({
      url: api.infoCenter.detail,
      method: "POST",
      data: {
        uid: user.uid,
        articleid: options.articleid,
        isadministrator: user.isadministrator
      },
      success: function(t) {
        var contents = t.data.content
        console.log()
        if (t.commont.length > 0) {
          t.commont.map(function(inex, item) {
            inex["ctime"] = WxParses.html2json(inex["ctime"], 'returnData');
          })
          _that.setData({
            commont: t.commont
          })
        }

        // for (var i in t.zujian)
        // {
        //   t.zujian[i].field = JSON.parse(t.zujian[i].field)
        //   var num = 0
        //   for (var n in t.zujian[i].field)
        //   {
        //     console.log(t.zujian[i].field.field_data_name)
        //     num++;
        //   }
        // }


        console.log(t.zujian)
        _that.setData({
          teit: t.data,
          teitList: t.data.readers,
          zujian: JSON.parse(t.data.zujian),
          zujian_list: t.zujian
        })

        console.log(_that.data)
        console.log('-------------------------------------------------------------------------')
        if(_that.data.zujian){
          // 时间线选择 数组
          for (var i = 0; i < _that.data.zujian.length; i++) {
            if (_that.data.zujian[i].type == 'date') {
              _that.data.date.push('')
            }
          }
        }
        WxParse.wxParse('article', 'html', contents, _that)
        if (t.data.votestatus == 1) {
          app.request({
            url: api.infoCenter.toupiao,
            method: "POST",
            data: {
              articleid: options.articleid,
              uid:user.uid,
            },
            success: function(t) {
              console.log(t);
            },
            fail: function() {
              console.log("fail")
            }
          })
        }
      },
      fail: function() {
        console.log("fail")
      }
    })

    // this.infoBjread(options)
  },
  //把信息标记为已读
  // infoBjread: function(options){
  //   var user = wx.getStorageSync("usr")
  //   app.request({
  //     url: api.infoCenter.bjread,
  //     method: "POST",
  //     data: {
  //         uid: user.uid,
  //      articleid: options.articleid
  //     },
  //     success: function(t){
  //       console.log("infoBjread=======>",t)
  //     }
  //   })
  // },
  //信息被那些人员阅读过
  // infoPeread: function(options){
  //   app.request({
  //     url: api.infoCenter.peread,
  //     method:"POST",
  //     data:{
  //         articleid: options.articleid,
  //     },
  //     success: function(res){
  //       var jsb = "技术部"
  //       console.log("infoPeread========>",res)  
  //     }
  //   })

  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})